package com.witmore.skyline.web.controller;

import com.witmore.skyline.web.config.GlobalAccountSession;
import com.witmore.skyline.web.controller.vo.param.DeviceBindDriverParam;
import com.witmore.skyline.web.controller.vo.param.DeviceEditParam;
import com.witmore.skyline.web.controller.vo.param.DeviceListParamVO;
import com.witmore.skyline.web.controller.vo.param.DeviceReportParam;
import com.witmore.skyline.web.controller.vo.param.DeviceSnParam;
import com.witmore.skyline.web.controller.vo.result.*;
import com.witmore.skyline.web.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 设备相关接口
 * Author: 豆子高
 * Date: 2025/2/24 14:26
 */
@RestController
@RequestMapping("api/device/")
public class DeviceController{
    @Autowired
    private DeviceService deviceService;

    /**
     * 设备列表同步
     */
    @GetMapping(value = "deviceListSync", name = "设备列表同步")
    public ResponseResult<Void> deviceListSync() {
        deviceService.getAndSaveDeviceListFromCloudm();
        return ResponseResult.success();
    }

    /**
     * 设备详情同步
     */
    @GetMapping(value = "deviceDetailSync", name = "设备详情同步")
    public ResponseResult<Void> deviceDetailSync(DeviceSnParam deviceSnParam) {
        deviceService.deviceFieldMaintenanceFromCloudm(deviceSnParam.getDeviceSn());
        return ResponseResult.success();
    }

    /**
     * 设备列表接口（分页） V2.1
     */
    @GetMapping(value = "deviceListPage", name = "设备列表接口（分页）")
    public ResponsePageResult<DeviceListResultVO> deviceListPage(DeviceListParamVO deviceListParamVO) {
        AccountSession accountInfo = GlobalAccountSession.getUserInfo();;
        deviceListParamVO.setOrgId(accountInfo.getOrgId());
//        deviceListParamVO.setProjectId(accountInfo.getProjectId());
        return deviceService.deviceListPage(deviceListParamVO);
    }

    /**
     * 设备编辑接口 V2.1
     */
    @PostMapping(value = "edit" , name = "设备编辑接口")
    public ResponseResult<Void> edit( @Valid @RequestBody DeviceEditParam deviceEditParam) {
        AccountSession accountInfo = GlobalAccountSession.getUserInfo();;
        return deviceService.edit(accountInfo, deviceEditParam);
    }


    /**
     * 设备类型分布接口 V2.1
     */
    @GetMapping(value = "deviceCategoryDistribution", name = "设备类型分布接口")
    public ResponseResult<List<DeviceCategoryVO>> deviceCategoryDistribution() {
        AccountSession accountInfo = GlobalAccountSession.getUserInfo();;
        return deviceService.deviceCategoryDistribution(accountInfo);
    }


    /**
     * 设备概况信息接口
     */
    @GetMapping(value = "deviceDetail",name = "设备概况信息接口")
    public ResponseResult<DeviceDetailResultVO> deviceDetail(DeviceSnParam deviceSnParam) {
        AccountSession accountInfo = GlobalAccountSession.getUserInfo();
        return deviceService.deviceDetail(deviceSnParam, accountInfo);
    }

    /**
     * 设备日报列表接口
     */
    @GetMapping(value = "deviceDailyReportPage",name = "设备日报列表接口")
    public ResponsePageResult<DeviceDailyResultVO> deviceDailyReportPage(@Valid DeviceReportParam deviceDailyParamVO) {
        AccountSession accountInfo = GlobalAccountSession.getUserInfo();;
        deviceDailyParamVO.setOrgId(accountInfo.getOrgId());
        deviceDailyParamVO.setProjectId(accountInfo.getProjectId());
        deviceDailyParamVO.setPage(null == deviceDailyParamVO.getPage() ? 1 :  deviceDailyParamVO.getPage());
        deviceDailyParamVO.setSize(null == deviceDailyParamVO.getSize() ? 20 : deviceDailyParamVO.getSize());
        return deviceService.deviceDailyReportPage(deviceDailyParamVO);
    }

    /**
     * 设备地图统计接口
     */
    @GetMapping(value = "deviceMapStatistics", name = "设备地图统计接口")
    public ResponseResult<List<DeviceMapStatisticsVO>> deviceMapStatistics() {
        AccountSession accountInfo = GlobalAccountSession.getUserInfo();
        return deviceService.deviceMapStatistics(accountInfo);
    }

    /**
     * 绑定设备和驾驶员 V2.1
     */
    @PostMapping(value = "bindDriver", name = "绑定设备和驾驶员")
    public ResponseResult<Void> bindDriver(@Valid @RequestBody DeviceBindDriverParam param) {
        AccountSession accountInfo = GlobalAccountSession.getUserInfo();
        return deviceService.bindDriver(param, accountInfo);
    }

    /**
     * 解绑设备和驾驶员 V2.1
     */
    @PostMapping(value = "unbindDriver", name = "解绑设备和驾驶员")
    public ResponseResult<Void> unbindDriver(@RequestParam("deviceId") Integer deviceId) {
        AccountSession accountInfo = GlobalAccountSession.getUserInfo();
        return deviceService.unbindDriver(deviceId, accountInfo);
    }

    /**
     * 查询设备绑定的驾驶员详情 V2.1
     */
    @GetMapping(value = "getDriverByDevice", name = "查询设备绑定的驾驶员详情")
    public ResponseResult<DriverSimpleVO> getDriverByDevice(@RequestParam("deviceId") Integer deviceId) {
        return deviceService.getDriverByDevice(deviceId);
    }
}
